//
//  MeatMarketsHeader.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#define selfH TJ_StatusBarHeight_Define + 120

#import "MeatMainHeader.h"

@interface MeatMainHeader ()<UITextFieldDelegate>


/**
 MeatCityMainView 's LocationLab -> UILabel
 */
@property (nonatomic, strong) UILabel *locationLab;


/**
 MeatCityMainView 's ArrowIcon -> UIImageView
 */
@property (nonatomic, strong) UIImageView *arrowIcon;


/**
 MeatCityMainView 's MessageBtn -> UIButton
 */
@property (nonatomic, strong) UIButton *messageBtn;


/**
 MeatCityMainView 's NavView -> UIView
 */
@property (strong, nonatomic) UIView *navView;


/**
 MeatCityainView 's SearchField -> UITextField
 */
@property (strong, nonatomic) UITextField *searchTextField;


/**
 MeatCityainView 's HistoryScrollView -> UIScrollView
 */
@property (nonatomic, strong) UIScrollView *historyScrollView;


@end

@implementation MeatMainHeader

#pragma mark - lifecycle methods

- (void)setupUI{
    
    UIImage *image = [UIImage imageNamed:@"meat_buy_bg"];
    self.layer.contents = (__bridge id)image.CGImage;
    
    [self addSubview:self.locationLab];
    [self addSubview:self.arrowIcon];
    [self addSubview:self.messageBtn];
    [self addSubview:self.navView];
    [self addSubview:self.historyScrollView];
    
}

- (void)setupLayout{
    
    self.height = selfH;
    
    {
        _locationLab.mj_x = 20;
        _locationLab.mj_y = TJ_StatusBarHeight_Define + 10;
        _locationLab.size = CGSizeMake(300, 18);
        [self.locationLab sizeToFit];
    }
    
    
    {
        _arrowIcon.mj_y = _locationLab.mj_y;
        _arrowIcon.mj_x = _locationLab.right;
        _arrowIcon.size = CGSizeMake([self fitSize:18], [self fitSize:18]);
    }
    
    
    {
        _messageBtn.mj_y = _locationLab.mj_y;
        _messageBtn.size = CGSizeMake(18, 18);
        _messageBtn.mj_x = self.right - _messageBtn.width - [self fitSize:18];

    }

    
    {
        _navView.mj_x = 15;
        _navView.mj_y = _locationLab.bottom + 12;
        _navView.size = CGSizeMake(KScreenWidth - 30 , 36);
    }
    
    {
        _historyScrollView.mj_x = 15;
        _historyScrollView.mj_y = _navView.bottom;
        _historyScrollView.width = KScreenWidth - 15;
        _historyScrollView.height = 40;
    }
    
}



#pragma mark - setter methods

- (void)setKeys:(NSArray *)keys{
    
    CGFloat buttonX = 0;
    CGFloat buttonSpace = 20;

    if (keys && keys.count >0) {
        
        for(NSInteger i=0;i<keys.count;i++){
            
            NSString *string = keys[i];
            CGSize size = [string sizeWithFont:mFont(14)];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:string forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
            button.titleLabel.font = mFont(14);
            button.frame = CGRectMake(buttonX, 0, size.width, 40);
          
            @weakify(self);
            button.tapBlock = ^{
                @strongify(self);
                if (self.dealKeyBoardBlock) {
                    self.dealKeyBoardBlock(i);
                }
            };
            
            [self.historyScrollView addSubview:button];
            buttonX += buttonSpace + size.width;
        }
        
        CGFloat contentX = buttonX > self.historyScrollView.bounds.size.width ? buttonX :self.historyScrollView.bounds.size.width;
        self.historyScrollView.contentSize = CGSizeMake(contentX, 0);
        
    }
}


- (void)setLocation:(NSString *)location{
    
    self.locationLab.text = location;
    [self setupLayout];
}

#pragma mark - getter methods

- (UILabel *)locationLab{
    
    if (!_locationLab) {
        _locationLab = [UILabel makeLabelWithText:@"请重新定位" textColor:KWhiteColor font:mFont(15) textAliment:0];
        
        kWeakSelf(self);
        _locationLab.tapBlock = ^{
            if (weakself.dealLocationBlock) {
                weakself.dealLocationBlock();
            }
        };

        
    }
    return _locationLab;
}


- (UIImageView *)arrowIcon{

    if (!_arrowIcon) {
        _arrowIcon = [UIImageView makeImageByImageName:@"meat_buy_down_white" cornerRadius:0];
    }
    return _arrowIcon;
}


- (UIButton *)messageBtn{
    
    if (!_messageBtn) {
        _messageBtn = [UIButton makeButtonWithImageName:@"meat_buy_message"];
        
        kWeakSelf(self);
        _messageBtn.tapBlock = ^{
            if (weakself.dealMessageBlock) {
                weakself.dealMessageBlock();
            }
        };
    }
    return _messageBtn;
}


- (UIView *)navView
{
    if (!_navView) {
        _navView = [UIView makeViewBgColor:KWhiteColor cornerRadius:8];
        
        UIImageView *searchImgView = [UIImageView makeImageByImageName:@"meat_buy_search" cornerRadius:0];
        
        searchImgView.size = CGSizeMake([self fitSize:18], [self fitSize:18]);
        searchImgView.mj_x = (36 - searchImgView.width)/2.0;
        searchImgView.mj_y = (36 - searchImgView.height)/2.0;
        [_navView addSubview:searchImgView];
        
        [_navView addSubview:self.searchTextField];
    }
    return _navView;
}


- (UITextField *)searchTextField
{
    if (!_searchTextField) {
        
        _searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(34, 0, KScreenWidth-15-36-80, 36)];
        _searchTextField.placeholder = @" 输入店铺名或者部位进行搜索";
        _searchTextField.delegate = self;
        _searchTextField.textColor = UIColorFromRGB(0xB2B2B2);
        _searchTextField.font = mFont(14);
        
        kWeakSelf(self);
        _searchTextField.tapBlock = ^{
            if (weakself.dealSearchBlock) {
                weakself.dealSearchBlock();
            }
        };
    }
    return _searchTextField;
}


- (UIScrollView *)historyScrollView{
    
    if(!_historyScrollView){
        
        _historyScrollView = [[UIScrollView alloc]init];
        _historyScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _historyScrollView;
}


@end
